package com.dearbinge.data.service.impl.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserContactTagRelationService;
import com.dearbinge.data.api.im.UserContactTagService;
import com.dearbinge.data.dao.im.ImUserContactTagMapper;
import com.dearbinge.data.pojo.im.ImUserContactTag;
import com.dearbinge.data.pojo.im.vo.ImUserContactTagVO;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 好友标签实现层
 *
 * @author wes
 * @date 2019/2/27
 */
@Service(value = "userContactTagService")
public class UserContactServiceTagImpl implements UserContactTagService {

    @Autowired
    private ImUserContactTagMapper imUserContactTagMapper;
    @Autowired
    private UserContactTagRelationService userContactTagRelationService;
    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer delete(Integer id) {
        return imUserContactTagMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新
     *
     * @param userContactTag 好友标签
     * @return 结果
     */
    @Override
    public Integer update(ImUserContactTag userContactTag) {
        return imUserContactTagMapper.update(userContactTag);
    }

    /**
     * 插入
     *
     * @param userContactTag 好友标签
     * @return 结果
     */
    @Override
    public Integer insert(ImUserContactTag userContactTag) {


        //新建一个标签


        return imUserContactTagMapper.insert(userContactTag);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 结果对象
     */
    @Override
    public ImUserContactTag getById(Integer id) {
        return imUserContactTagMapper.getByPrimaryKey(id);
    }

    /**
     * 根据用户ID查询所有标签
     *
     * @param userId 用户id
     * @return 标签集
     */
    @Override
    public Page listByUserId(Integer userId,Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        //分页
        Page<ImUserContactTagVO> page = new Page<ImUserContactTagVO>(current, size);
        List<ImUserContactTagVO> userContactTag =  imUserContactTagMapper.listByUserId(userId,page);
        //标签好友数 标签好友名字
        for (ImUserContactTagVO imUserContactTagVO : userContactTag) {
            userContactTagRelationService.listByTagId(imUserContactTagVO.getId(),null,null);
        }
        //查出总条数
        Integer count =  imUserContactTagMapper.countByUserId(userId);
        //最大数
        page.setTotal(count);
        page.setRecords(userContactTag);
        return page;
    }



}
